/*
@File    :   OneWire.c
@Time    :   2023--CURRENT_MONTH-11 20:05:22
@Author  :   iget
@Version :   1.0
@Contact :   yonglonggeng@163.com
@License :   (C)Copyright 1994-2023, Liugroup-NLPR-CASIA
@Desc    :   delay  1 ms
@WeChat  :   ddgetget
*/
#include <REGX52.H>

sbit OneWire_DQ = P3^7;

unsigned char OneWire_Init(void)
{
    /*
     * Parameter		:
     * ReturnValue	:
     * Function		:init function
     * Date			:2023-11-11 20:12:06
     */
    unsigned char i;
    unsigned char AckBit;
    OneWire_DQ = 1;
    OneWire_DQ = 0;
    i = 247;
    while (--i);
    OneWire_DQ = 1;
    i = 32;
    while (--i);
    AckBit = OneWire_DQ;
    i = 247;
    while (--i)
    { // delay 500us
        /* code */
    };
    return AckBit;
}

void OneWire_SendBit(unsigned char Bit)
{
    /*
     * Parameter		:
     * ReturnValue	:
     * Function		:write onw bit
     * Date			:2023-11-11 20:24:20
     */
    unsigned char i;
    OneWire_DQ = 0;

    i = 4;
    while (--i);
    OneWire_DQ = Bit;
    i = 24;
    while (--i);
    OneWire_DQ=1;
}

unsigned char OneWire_ReceiveBit(void)
{
    /*
     * Parameter		:
     * ReturnValue	:
     * Function		:recieve bit
     * Date			:2023-11-11 20:27:28
     */
    unsigned char i;
    unsigned char Bit;
    OneWire_DQ = 0;
    i = 2;
    while (--i);
    OneWire_DQ = 1;
    i = 2;while(--i){};

    Bit = OneWire_DQ;
    i = 24;
    while (--i);
    return Bit;
}

void OneWire_SendByte(unsigned char Byte)
{
    unsigned char i;
    for (i = 0; i < 8; i++)
    {
        /* code */
        OneWire_SendBit(Byte & (0x01<<i));
    }
}

unsigned char OneWire_ReceiveByte(void)
{
    unsigned char i;
    unsigned char Byte = 0x00;
    for (i = 0; i < 8; i++)
    {
        if (OneWire_ReceiveBit()){Byte |= (0x01<<i);}
    }
    return Byte;
}